package com.xiaoma.thread.interrupt;

/**
 * @author xiaomacdut
 * @date 2020年3月21日 下午12:10:46
 */
public class ThreadDemo{
    
    public static void main(String [] args) throws InterruptedException {
        Runnable myRunnable = new MyThread(); // 创建一个Runnable实现类的对象
        Thread thread = new Thread(myRunnable, "线程A");// 调用start()方法使得线程进入就绪状态
        thread.start();
        Thread.sleep(1000);

         if(!thread.isInterrupted()){
            // 如果线程没有被中断
            System.out.println("准备中断线程");
            thread.interrupt();
        }
        
        
    }
    

    
    static class MyThread implements Runnable{
        
        @Override
        public void run(){
            try{
                System.out.println("执行业务");
                Thread.sleep(10000);
                System.out.println("after sleep......");
            }catch(InterruptedException e){
                System.out.println(Thread.currentThread().getName()+"线程被中断");
            }
        }
    }
    
}

